<?php

namespace App\Http\Controllers\Api;

use App\Cinema;
use App\FilmTicket;
use App\Screen;
use App\Seats;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ScreenController extends BaseController
{
    public $user;

    public function __construct(Request $request)
    {
        parent::__construct();
        $this->user = $this->is_login($request);
    }

    //影厅信息
    public function index(Request $request){

        if (!$this->user['data']->can('screen_manager')){
            return error('NoPermission');
        }

        $cinema_code = $this->user['data']['cinema_code'];
        if(!$id = trim($request->id)){
            return error('影厅id不能为空');
        }
        $info=Screen::where('status','!=',3)->where(['cinema_code'=>$cinema_code])->find($id);
        if (!$info){
            return error('影厅不存在');
        }
        return success($info,'查询成功');
    }

    //查看影厅列表
    public function lists(Request $request){

        if (!$this->user['data']->can('screen_manager')){
            return error('NoPermission');
        }

        $where['cinema_code'] = $this->user['data']['cinema_code'];
        $data['screen_list'] = Screen::where('status','!=',3)->where($where)->get();
        return success($data,'查询成功');
    }

    /**
     * 添加影厅信息
     */
    public function add_screen(Request $request){

        if (!$this->user['data']->can('screen_manager')){
            return error('NoPermission');
        }

        $cinema_code = $this->user['data']['cinema_code'];
        if (!$name=trim($request->name)){
            return error('影厅名称必填');
        }
        if (!$type=trim($request->type)){
            return error('影厅类型必填');
        }
        if (!$status=trim($request->status)){
            return error('影厅状态必填');
        }
        $res = Screen::where(['name'=>$name,'cinema_code'=>$cinema_code])->where('status','!=',3)->first();
        if($res){
            return error('影厅已存在');
        }
        $screen = new Screen();
        $screen->cinema_code=$cinema_code;
        $screen->code=getScreenCode($cinema_code);
        $screen->name=$request->input('name');
        $screen->type=$request->input('type');
        $screen->status = $request->input('status');
        \DB::beginTransaction();
        try{
            $res = Cinema::where('code','=',$cinema_code)->increment('screen_count');
            if ($res&&$screen->save()){
                \DB::commit();
                logs($this->user,'添加影厅'.$screen->name.'成功');
                return success($screen->code,'添加成功');
            }
        }catch (\Exception $e){
            \DB::rollBack();
            logs($this->user,'添加影厅'.$screen->name.'失败');
            return error('添加失败');
        }
    }

    /**
     * 编辑影厅
     */
    public function edit_screen(Request $request){

        if (!$this->user['data']->can('screen_manager')){
            return error('NoPermission');
        }

        $cinema_code = $this->user['data']['cinema_code'];
        if (!$id = trim($request->id)){
            return error('ID信息缺失');
        }
        if (!$name = trim($request->name)){
            return error('影厅名称缺失');
        }
        $screen = Screen::where('status','!=','3')->find($id);
        if(!$screen){
            return error('影厅不存在');
        }
        $res = Screen::where(['name'=>$name,'cinema_code'=>$cinema_code])->where('id','!=',$id)->first();
        if($res){
            return error('影厅名称已存在');
        }
        $screen->name=$request->input('name');
        $screen->type=$request->input('type');
        $screen->status = $request->input('status');
        if($screen->save()){
            logs($this->user,'编辑影厅'.$screen->name.'成功');
            return success($screen->id,'编辑成功');
        }else{
            logs($this->user,'编辑影厅'.$screen->name.'失败');
            return error('编辑失败');
        }
    }

    /**
     * 删除影厅
     */
    public function delete_screen(Request $request){

        if (!$this->user['data']->can('screen_manager')){
            return error('NoPermission');
        }

        $cinema_code = $this->user['data']['cinema_code'];
        if (!$request->input('id')){
            return error('ID信息缺失');
        }
        $screen = Screen::where('status','!=','3')->find($request->input('id'));
        if(!$screen){
            return error('影厅不存在');
        }
        $screen->status = 3;
        \DB::beginTransaction();
        try{
            $res = Cinema::where('code','=',$cinema_code)->increment('screen_count','-1');
            if($res&&$screen->save()) {
                \DB::commit();
                logs($this->user,'删除影厅'.$screen->name.'成功');
                return success($screen->id,'删除成功');
            }
        }catch (\Exception $e){
            \DB::rollBack();
            logs($this->user,'删除影厅'.$screen->name.'失败');
            return error('删除失败');
        }
    }

    /**
     * @param Request $request
     * @return array
     * 查询影厅座位信息
     */
    public function seat_info(Request $request){

        if (!$this->user['data']->can('screen_manager')){
            return error('NoPermission');
        }

        if (!$screen_code = $request->input('screen_code')){
            return error('影厅编码缺失');
        }

        if(Screen::where('code','=',$screen_code)->get()->count()==0){
            return error('未找到对应影厅');
        }

        $data = Seats::where('status','!=',2)->where(['screen_code'=>$screen_code])->get();

        return success($data,'查询座位信息成功');
    }

    /*
     * 添加、编辑座位
     */
    public function add_seat(Request $request){

        if (!$this->user['data']->can('screen_manager')){
            return error('NoPermission');
        }
        if (!$screen_code = $request->input('screen_code')){
            return error('影厅编码缺失');
        }
        if($screen = Screen::where('code','=',$screen_code)->get()->count()==0){
            return error('未找到对应影厅');
        }

        if (!$seats = $request->input('seats')){
            return error('添加座位不能为空');
        }

        $seat_arr = json_decode($seats,true);
        $fix_count = 0;

        $fix_seat = [];
        foreach ($seat_arr as $key=>$val){
            if ($val['row_num']==''){
                return error('座位行号缺失');
            }

            if ($val['column_num']==''){
                return error('座位列号缺失');
            }

            if ($val['group_code']==''){
                return error('座位类型缺失');
            }

            if (!is_numeric($val['x_coord'])){
                return error('座位横坐标缺失');
            }

            if (!is_numeric($val['y_coord'])){
                return error('座位纵坐标缺失');
            }

            if (!is_numeric($val['status'])){
                return error('座位状态缺失');
            }
            if($val['status']==3){
                $fix_count+=1;
            }
            $seat_code = getSeatCode($screen_code,$val['x_coord'],$val['y_coord']);
            $seat_arr[$key]['code'] = $seat_code;
            $seat_arr[$key]['screen_code'] = $screen_code;

            if ($val['status']==3){
                $fix_seat[] = $seat_code;
            }
        }

        \DB::beginTransaction();
        try{
            Seats::where(['screen_code'=>$screen_code])->delete();
            $res_seat = Seats::insert($seat_arr);
            $res_screen = Screen::where(['code'=>$screen_code])->update(['seat_count'=>count($seat_arr),'fix_count'=>$fix_count]);

            $res_update_film_ticket = FilmTicket::where('start_time','>',date('Y-m-d H:i:s'))
                ->where('status','=',1)
                ->whereIn('seat_code',$fix_seat)
                ->update(['status'=>5]);
            if($res_seat&&$res_screen){
                \DB::commit();
                logs($this->user,'编辑影厅编码：'.$screen_code.'座位成功');
                return success('','编辑成功');
            }
        }catch (\Exception $e){
            \DB::rollBack();
            logs($this->user,'编辑影厅编码：'.$screen_code.'座位失败');
            return error('编辑座位失败');
        }

    }

    /**
     * 编辑座位
     */
    public function edit_seat(Request $request){

        if (!$this->user['data']->can('screen_manager')){
            return error('NoPermission');
        }
        if (!$screen_code = $request->input('screen_code')){
            return error('影厅编码缺失');
        }
        if(Screen::where('code','=',$screen_code)->get()->count()==0){
            return error('未找到对应影厅');
        }

        if (!$seats = $request->input('seats')){
            return error('编辑座位信息不能为空');
        }

        $seat_arr = json_decode($seats,true);
        foreach ($seat_arr as $val){

            if (!isset($val['id'])){
                return error('座位ID号缺失');
            }

            if ($val['row_num']==''){
                return error('座位行号缺失');
            }

            if ($val['column_num']==''){
                return error('座位列号缺失');
            }

            if ($val['group_code']==''){
                return error('座位类型缺失');
            }

            if (!is_numeric($val['x_coord'])){
                return error('座位横坐标缺失');
            }

            if ($val['status']==''){
                return error('座位状态必填');
            }

            $res = Seats::where(['id'=>$val['id'],'screen_code'=>$screen_code])->where('status','!=',3)->get();
            if ($res->count()==0){
                return error('座位不存在!');
            }

            Seats::where(['id'=>$val['id'],'screen_code'=>$screen_code])->update($val);
        }

        return success('','更新成功');

    }

    /**
     * @param Request $request
     * 需要跟排场售票关联在一起
     */
    public function delete_seat(Request $request){

        if (!$this->user['data']->can('screen_manager')){
            return error('NoPermission');
        }

        if (!$screen_code = $request->input('screen_code')){
            return error('影厅编码缺失');
        }
        if(Screen::where('code','=',$screen_code)->get()->count()==0){
            return error('未找到对应影厅');
        }
        if (!$ids = trim($request->input('ids'))){
            return error('删除座位信息缺失');
        }
        $arr_id = explode(",",$ids);
        if(Seats::whereIn('id',$arr_id)->update(['status'=>3])){
            return success('','更新成功');
        }else{
            return error('更新失败');
        }

    }
}
